// cur = 0 mean timer is off state
var cur = 0;
var itr = 0;
var fresh = 1;

function showPopup(){
	var minutes = Math.floor( itr/60 ) ;
	var seconds = ( itr%60 ) ;
	var showMinutes = (minutes<10 ? "0" : "")+minutes;
	var showSeconds = (seconds<10 ? "0" : "")+seconds;
	if (confirm("Time Spent: "+showMinutes+"m: "+showSeconds+"s\nClear the record!")==true){
	    itr=0;
	}
}
function updateIcon(tabId) {
	chrome.browserAction.setIcon({path: ( cur==0? "stop":"start") + ".png"});
	cur^=1;
	if(cur==1&&fresh==0){
		showPopup();
	}
	fresh=0;
}
function showTimer(){
	var minutes = Math.floor( itr/60 ) ;
	var seconds = ( itr%60 );
	var showMinutes = (minutes<10 ? "0" : "")+minutes;
	var showSeconds = (seconds<10 ? "0" : "")+seconds;
	chrome.browserAction.setBadgeText ( { text: showMinutes+":"+showSeconds } );
	if(cur==0){
		itr++;
	}
}
chrome.browserAction.onClicked.addListener(updateIcon);
updateIcon();
setInterval( showTimer, 1000 );

